<?php 
//I.F. Nuevo: 2010-03-28; ramarin
require_once('../../../config.php');
//require_once($CFG->dirroot.'/course/lib.php');
require_once($CFG->libdir.'/adminlib.php');
require_once($CFG->dirroot.'/'.$CFG->admin.'/report/moosisca/managecategorieslib.php');

$systemcontext = get_context_instance(CONTEXT_SYSTEM);

if (!has_capability('report/moosisca:managecategories', $systemcontext)) {
	error('You do not have the required permission to manage categories.');
}

admin_externalpage_setup('moosisca_managecategories');
admin_externalpage_print_header();
echo '<link rel="stylesheet" type="text/css" href="'.$CFG->wwwroot.'/'.$CFG->admin.'/report/moosisca/styles.php" />';

if ($frm = data_submitted() and confirm_sesskey()) {
  $catIds = array();
  
  foreach($frm as $key => $input)
  {
    $pos = strpos($key, "chkAllow_");
    
    if($pos !== false)
    {
      $catIds[] = substr($key, strpos($key, '_') + 1, strlen($key) - strpos($key, '_'));
    }
  }
  
  categories_moosisca_remove();
  
  foreach($catIds as $id)
  {
    $c1 = "txtC1_".$id;
    $c2 = "txtC2_".$id;
    
    category_moosisca_add($id, $frm->$c1, $frm->$c2);
  }  
    
  /// and redirect back to the course listing.
  notice(get_string('successfulloperation', 'report_moosisca'));
}

  $displaylist = array();
  $parentlist = array();

  $displaylist[0] = get_string('top');
  make_categories_list($displaylist, $parentlist);
  
  echo <<<EOF
    <script type="text/javascript">
      function showHideCategoryTextbox(categoryId){
        var c1 = document.getElementById('txtC1_' + categoryId);
        var c2 = document.getElementById('txtC2_' + categoryId);
        
        if(c1.style.display == 'none'){
          c1.style.display = '';
          c2.style.display = '';
        }
        else{
          c1.style.display = 'none';
          c2.style.display = 'none';
        }
      }
      
      function checkField (categoryId) {
         var check = document.getElementById('chkAllow_' + categoryId);
         
        if(check.checked){
          check.checked = false;
        }
        else{
          check.checked = true;
        }
        
        showHideCategoryTextbox(categoryId);
      }
    </script>
EOF;

  echo '<form id="categoriesform" method="post" action="managecategories.php">';
  echo '<input type="hidden" name="sesskey" value="'.sesskey().'"/>';
  
  print_heading(get_string('categories'));
  
  echo '<table class="generalbox boxaligncenter">';
  
  print_category_edit(NULL, $displaylist, $parentlist);

  echo '</table>';
  
  echo '<div class="buttons">';
  echo '  <div class="button">';
  if (has_capability('report/moosisca:managecategories', $systemcontext)) {
    echo '<input type="submit" name="btnSave" value="'.get_string('save', 'report_moosisca').'" />';
  }
  echo '  </div>';
  echo '</div>';
  echo '</form>';
  
  admin_externalpage_print_footer();

function print_category_edit($category, $displaylist, $parentslist, $depth=-1) {
/// Recursive function to print all the categories ready for editing

  global $CFG, $USER;

  if (!empty($category)) {

      if (!isset($category->context)) {
          $category->context = get_context_instance(CONTEXT_COURSECAT, $category->id);
      }
      
      echo '<tr onmouseover="this.style.background=\'#ffffcc\'" onmouseout="this.style.background=\'transparent\'">';
      
      echo '<td align="left">';

      if($category->category_id != '')
      {
        echo '  <input type="text" id="txtC1_'.$category->id.'" name="txtC1_'.$category->id.'" size="2" value="'.$category->cod1.'" />';
        echo '  <input type="text" id="txtC2_'.$category->id.'" name="txtC2_'.$category->id.'" size="2" value="'.$category->cod2.'" />';
      }
      else
      {
        echo '  <input type="text" id="txtC1_'.$category->id.'" name="txtC1_'.$category->id.'" size="2" style="display:none" />';
        echo '  <input type="text" id="txtC2_'.$category->id.'" name="txtC2_'.$category->id.'" size="2" style="display:none" />';
      }
      
      echo '</td>';

      if($category->category_id != '')
      {
        echo '<td><input type="checkbox" id="chkAllow_'.$category->id.'" name="chkAllow_'.$category->id.'" onClick="showHideCategoryTextbox('.$category->id.')"  checked="checked" /></td>';
      }
      else 
      {
        echo '<td><input type="checkbox" id=chkAllow_'.$category->id.' name="chkAllow_'.$category->id.'" onClick="showHideCategoryTextbox('.$category->id.')" /></td>';
      }

      echo '<td align="left" class="name" onclick="checkField(\''.$category->id.'\')">';
      for ($i=0; $i<$depth;$i++) {
          echo '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
      }
      $linkcss = $category->visible ? '' : ' class="dimmed" ';
      echo format_string($category->name);
      echo '</td>';
      

      echo '</tr>';
  } else {
      $category->id = '0';
  }

  if ($categories = get_categories_moosisca($category->id)) {   // Print all the children recursively
    $countcats = count($categories);
    $count = 0;
    foreach ($categories as $cat) {
      $count++;
      if ($count == $countcats) {
          $last = true;
      }

      print_category_edit($cat, $displaylist, $parentslist, $depth+1);
    }
  }  
}
?>